local Config = require("Config")
local Human = require("core.Human")
local MakeTencentCommonParam = require("tencentApi.Define").MakeTencentCommonParam
local Split = require("tencentApi.Define").Split
local TaskMarket = require("task.TaskMarket")
local RoleDBLogic = require("role.RoleDBLogic")
local Json = require("common.Json")
local DeliverDefine = require("tencentApi.Define")
local Log = require("common.Log")
local RoleFFI  = require("role.RoleFFI")


--用于交叉推广任务后通知平台用户已完成任务
function informTaskCompleted(human, contractid, step)
	  s2aParam = {}
    local now = os.time()

    local pf_info = human.pf_info
    if (pf_info == nil) then
        return
    end

    local callback_general_param = {}
    callback_general_param.account = human.account
    callback_general_param.name = human.name
    s2aParam.callback_general_param = callback_general_param

    local api_param = MakeTencentCommonParam(pf_info)
    api_param.zoneid = pf_info.zoneid
    api_param.ts = now
    api_param.inform_type = 1
    api_param.contractid = contractid
    s2aParam.api_param = api_param
    
    s2aParam.api_url = "/v3/pay/inform_task_completed"
    s2aParam.api_cbMethod = "OnInformTaskCompletedReturn"
    Log.write(Log.LOGID_OSS_TASK_MARKET, human._id, human.account, human.name, contractid, 35)
		s2aParam.version = "v3"
		s2aParam.step = step
		print(Json.Encode(s2aParam))
--		Log.write(Log.LOGID_OSS_TASK_MARKET, human._id, human.account, human.name, Json.Encode(s2aParam), 36)
		_G.thread_http.send(DeliverDefine.TASK_V3,Json.Encode(s2aParam))
end

function openID2Uuid(openID)
	local humanAccount = Config.SVR_INDEX .. "-" .. openID
	if Human.onlineAccount[humanAccount] then
		return Human.onlineAccount[humanAccount]._id
	else
		return RoleDBLogic.getUuidByAccount(humanAccount)
	end
end


local fields = {
account=1,
name=1,
appContractId = 1,
appContractIdStatus = 1,
}

function onAward(oJsonInput)
	local respond = {}
	respond.ret = 0
	respond.msg = "OK"
	local openid = oJsonInput.openid
	local contractid = oJsonInput.contractid
	local payitem = oJsonInput.payitem
	local item_id = tonumber(payitem) or 0
	local item_cnt = 1

	if item_id == 0  or item_cnt == 0 then
		return respond
  end
	local uuid = openID2Uuid(openid)
	if uuid == nil or uuid == "" then
		respond.ret = 1
		respond.msg = "no this user"
		return respond
	end
	respond.ret,respond.msg = TaskMarket.onTaskMarketDeliver(uuid, contractid, item_id, item_cnt)
	return respond
end

function onCheckAward(oJsonInput)
	local respond = {}
	respond.ret = 0
	respond.msg = "OK"
	local openid = oJsonInput.openid
	local contractid = oJsonInput.contractid
	local step = oJsonInput.step
	if step == nil then
		respond.ret = 103
		respond.msg = "param err"
		return respond
	end
	local payitem = oJsonInput.payitem
	local item_id = tonumber(payitem) or 0
	local item_cnt = 1

	if item_id == 0  or item_cnt == 0 then
		return respond
  end

	local uuid = openID2Uuid(openid)
	if uuid == nil or uuid == "" then
		respond.ret = 1
		respond.msg = "no this user"
		return respond
	end
	local appContractId  
	local target = Human.onlineUuid[uuid]
	local online = true
	if target == nil then
		target = {}
		online = false
		target.db = RoleDBLogic.getDb(uuid, fields)
		appContractId = target.db.appContractId
  else
    appContractId = RoleFFI.ffi_string(target.db.appContractId)
	end
  if appContractId == "" then
    respond.ret = 2
    respond.msg = "NOT FINISH"
    return respond
  end
  local status = TaskMarket.getStatusByStep(target.db.appContractIdStatus, step)
	if status == TaskMarket.HAS_GET then
		respond.ret = 3
		respond.msg = "HAS GET"
	elseif status == TaskMarket.CAN_GET then
		respond.ret,respond.msg = TaskMarket.onTaskMarketDeliver(uuid, contractid, item_id, item_cnt)
		TaskMarket.setStatusByStep(target, step, TaskMarket.HAS_GET)
		if not online then
		  RoleDBLogic.saveRoleSset(target.db)
		end
	else
	  respond.ret = 2
    respond.msg = "NOT FINISH"
	end	
	return respond
end

function onCheck(oJsonInput)
	local respond = {}
	respond.ret = 0
	respond.msg = "OK"
	local openid = oJsonInput.openid
	local contractid = oJsonInput.contractid
	local step = oJsonInput.step
	if step == nil then
		respond.ret = 103
		respond.msg = "param err"
		return respond
	end
	local payitem = oJsonInput.payitem
	local item_id = tonumber(payitem) or 0
	local item_cnt = 1
	if item_id == 0  or item_cnt == 0 then
		return respond
  end
    
	local uuid = openID2Uuid(openid)
	if uuid == nil or uuid == "" then
		respond.ret = 1
		respond.msg = "no this user"
		return respond
	end
	
	local appContractId  
	local target = Human.onlineUuid[uuid]
	if target == nil then
		target = {}
		target.db = RoleDBLogic.getDb(uuid, fields)
		appContractId = target.db.appContractId
  else
    appContractId = RoleFFI.ffi_string(target.db.appContractId)
	end
  
	if appContractId == "" then
		respond.ret = 2
		respond.msg = "NOT FINISH"
		return respond
  end
  local status = TaskMarket.getStatusByStep(target.db.appContractIdStatus, step)
	if status == TaskMarket.HAS_GET then
		respond.ret = 3
		respond.msg = "HAS GET"
	elseif status == TaskMarket.CAN_GET then
		respond.ret = 0
		respond.msg = "OK"
  else
    respond.ret = 2
    respond.msg = "NOT FINISH"
	end
	return respond
end
